package xyz.thoughtset.viewer.models.deepseek;

import lombok.NonNull;
import org.springframework.ai.chat.model.ChatModel;
import org.springframework.ai.deepseek.DeepSeekChatModel;
import org.springframework.ai.deepseek.DeepSeekChatOptions;
import org.springframework.ai.deepseek.api.DeepSeekApi;
import org.springframework.stereotype.Component;
import xyz.thoughtset.viewer.common.ai.model.entity.AiNode;
import xyz.thoughtset.viewer.common.ai.model.entity.ModelParam;
import xyz.thoughtset.viewer.common.ai.model.entity.purpose.ChatModelSetting;
import xyz.thoughtset.viewer.common.ai.model.factory.ModelBuilder;

import java.util.Optional;

@Component
public class DeepSeekBuilder extends ModelBuilder {
    public DeepSeekBuilder() {
        super("DeepSeek");
    }

    @Override
    public boolean wasDefault() {
        return true;
    }

    @Override
    public ChatModel buildMode(AiNode node, ModelParam modelParam) {
        DeepSeekChatOptions.Builder builder = DeepSeekChatOptions.builder()
                .model(modelParam.getModel());
        ChatModelSetting settingObj = (ChatModelSetting) modelParam.getModelArgs();
//        if(settingObj!=null) {
//            if (settingObj.getMaxTokens() != null) {
//                builder.maxTokens(settingObj.getMaxTokens().intValue());
//            }
//            if (settingObj.getTemperature() != null) {
//                builder.temperature(settingObj.getTemperature());
//            }
//        }
        Optional.ofNullable(settingObj).ifPresent(setting -> {
            setOptionalValue(setting.getMaxTokens(), value -> builder.maxTokens(value.intValue()));
            setOptionalValue(setting.getTemperature(), builder::temperature);
        });
        DeepSeekChatOptions options = builder.build();
        DeepSeekApi api = DeepSeekApi.builder()
                .baseUrl(node.getBaseUrl())
                .apiKey(node.getApiKey()).build();
        ChatModel chatModel = DeepSeekChatModel.builder()
                .defaultOptions(options)
                .deepSeekApi(api)
                .build();
        return chatModel;
    }
}
